package com.zjmzxfzhl.modules.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.base.BaseServiceImpl;
import com.zjmzxfzhl.common.exception.AppException;
import com.zjmzxfzhl.modules.purchase.entity.PurchaseMaterial;
import com.zjmzxfzhl.modules.purchase.mapper.PurchaseMaterialMapper;
import com.zjmzxfzhl.modules.purchase.service.PurchaseMaterialService;
import com.zjmzxfzhl.modules.purchase.service.PurchaseOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Service
 *
 * @author 庄金明
 */
@Service
public class PurchaseMaterialServiceImpl extends BaseServiceImpl<PurchaseMaterialMapper, PurchaseMaterial>
        implements PurchaseMaterialService {

    public static Integer INIT_VERSION = 1;

    @Autowired
    private PurchaseOrderDetailService detailService;

    @Override
    public IPage<PurchaseMaterial> list(IPage<PurchaseMaterial> page, PurchaseMaterial purchaseMaterial) {
        return page.setRecords(baseMapper.list(page, purchaseMaterial));
    }

    @Override
    public PurchaseMaterial validPurchaseMaterial(PurchaseMaterial material) {
        if (!StringUtils.isEmpty(material.getMaterialName()) && null != material.getUnitPrice()) {
            PurchaseMaterial filter = new PurchaseMaterial();
            filter.setMaterialName(material.getMaterialName());
            filter.setMaterialSpecification(material.getMaterialSpecification());
            List<PurchaseMaterial> dbMaterialList = this.list(new QueryWrapper<>(filter));
            if (CollectionUtils.isEmpty(dbMaterialList)) {
                //插入新物料
                material.setMaterialVersion(INIT_VERSION);
                this.save(material);
                return material;
            } else {
                //老物料  判断价格是否一致
                return findOrInsert(dbMaterialList, material);
            }
        } else {
            throw new AppException("校验设备信息缺少关键信息：设备名称或设备单价");
        }

    }

    @Override
    public PurchaseMaterial findOrInsert(List<PurchaseMaterial> dbMaterialList, PurchaseMaterial material) {
        for (PurchaseMaterial dbMaterial : dbMaterialList) {
            //查找对应的老物料 ，价格一致。
            if (dbMaterial.getUnitPrice().floatValue() == material.getUnitPrice().floatValue()) {
                //匹配到目标物料
                //1 判断物料单位
                if (!StringUtils.isEmpty(material.getUnit())) {
                    dbMaterial.setUnit(material.getUnit());
                    this.updateById(dbMaterial);
                }
                //2 判断物料编码
                if (!StringUtils.isEmpty(material.getMaterialCode())) {
                    if (!StringUtils.isEmpty(dbMaterial.getMaterialCode())) {
                        if (!material.getMaterialCode().equals(dbMaterial.getMaterialCode())) {
                            if (dbMaterial.getMaterialCode().contains(material.getMaterialCode())) {
                                //不处理
                            } else if (material.getMaterialCode().contains(dbMaterial.getMaterialCode())) {
                                //更新
                                dbMaterial.setMaterialCode(material.getMaterialCode());
                                this.updateById(dbMaterial);
                            } else {
                                throw new AppException("导入的物料编码与库里的不一致：" + material.getMaterialCode() + "-" + dbMaterial.getMaterialCode());
                            }
                        }
                    } else {
                        dbMaterial.setMaterialCode(material.getMaterialCode());
                        this.updateById(dbMaterial);
                    }
                }
                return dbMaterial;
            }
        }
        //未匹配到
        //插入新增：
        Integer maxVersion = dbMaterialList.stream().map(e -> e.getMaterialVersion()).max(Integer::compareTo).get();
        material.setMaterialVersion(maxVersion + 1);
        this.save(material);
        return material;
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        for (Long materialId : ids) {
            //校验是否被审批单引用
            Integer detailCnt = detailService.countByMaterialId(materialId);
            if (detailCnt.intValue() > 0) {
                throw new AppException("物料被审批单引用，无法删除");
            }
            this.removeById(materialId);
        }
    }

}
